//
//  CollectionViewCell.m
//  freeLance
//
//  Created by Igor Karpenko on 3/28/13.
//  Copyright (c) 2013 Mozi Development. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "CDImages.h"
#import "UIImage+Scale.h"

#import "CollectionViewCell.h"

@implementation CollectionViewCell

-(id)initWithCoder:(NSCoder *)aDecoder {
	self = [super initWithCoder:aDecoder];
	if (!self) {
		return nil;
	}
	
	return self;
}


-(void) awakeFromNib {
	[super awakeFromNib];
    UIColor *borderColor = RGB(189.0f, 195.0f, 199.0f);
	
	if (IS_IPAD) {
		UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cll_cell_background"]];
		[_contentBackgroundView addSubview:backgroundImageView];
		[_contentBackgroundView sendSubviewToBack:backgroundImageView];
	} else {
        _contentBackgroundView.layer.borderWidth = 1.0f;
        _contentBackgroundView.layer.borderColor = [borderColor CGColor];
    }
	
	_generalImageView.layer.borderWidth = 1.0f;
	_generalImageView.layer.borderColor = [borderColor CGColor];
}


-(void) setProduct:(CDProducts *)product {
	_product = product;
	
	_titleLabel.text = _product.name;
	_priceLabel.text = [NSString stringWithFormat:@"%@ р.", _product.price];
	
    
    NSString *imagePath = [[product.images anyObject] imagePath];
    
    NSArray *images = [product.images allObjects];
    for (CDImages *image in images) {
        if ([[[[image.url stringByDeletingPathExtension] lastPathComponent] componentsSeparatedByString:@"_"] count] == 2) {
            imagePath = image.imagePathThumbnail200;
        }
    }

    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^() {
		NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
		UIImage *generalImage = [UIImage imageWithData:imageData];
		
		dispatch_async(dispatch_get_main_queue(), ^() {
            _generalImageView.image = generalImage;
        });
	});
}

@end